
/**********************************************************

file:  Communication_protocal.c

Author:	  albert

**********************************************************/

#include "Communication_protocal.h"
#include "atmega128_usart.h"
#include "Xbee.h"
#include "Camera_LS_Y201.h"
#include "spi.h"


//only has AT and CMD, not parameters
struct protocal
{
	unsigned char test[13]; //testing data from remote to pc
	unsigned char size[4]; //protocal for returning image size
	unsigned char take_picture[4]; //protocal for returning image
	unsigned char OnetoNigh[9]; //testing data from pc to remote
	unsigned char motor_direction[3]; //Receiving 4 bytes, last byte indicating direction
} myProtocal =
{
	{
		0x58, 0x4A, 0x43, 0x4B, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39
	}
	,
	{
		0x57, 0x4A, 0x53, 0x5A
	}
	,
	{
		0x58, 0x4A, 0x49, 0x4D
	}
	,
	{
		0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39
	}
	,
	{
		0x59, 0x4A, 0x48
	}
};

/**************************************************************************
*                                                                        *
**************************************************************************/

unsigned char Communication_test()
{
	unsigned pass = 1;
	usart0_count = 0;
	if (usart_mode == 0)
	{
		while (usart0_count != 13)
		{
			USART_Receive(xbee_usart_num);
		}
	}
	else
	{
		while (usart0_count != 13)
		;
	}
	for (unsigned char i = 0; i < 13; i++)
	{
		if (myProtocal.test[i] != usart0_rec_buff[i])
		pass = 0;
	}
	if (pass == 1)
	{
		USART_BYTES_Transmit(xbee_usart_num, myProtocal.OnetoNigh, sizeof(myProtocal.OnetoNigh));
	}
	usart0_count = 0;
	return pass;
}

/**************************************************************************
*                                                                        *
**************************************************************************/

unsigned char sizeReturn()
{
	unsigned pass = 1;
	/*usart0_count = 0;
	if (usart_mode == 0)
	{
		while (usart0_count != 4)
		{
			USART_Receive(xbee_usart_num);
		}
	}
	else
	{
		while (usart0_count != 4)
		;
	} */
	// for (unsigned char i = 0; i < 4; i++)
	for (unsigned char i =1; i < 4; i++)
	{
		if (myProtocal.size[i] != usart0_rec_buff[i])
		pass = 0;
	}
	if (Camera_takePicture() != 1)
	return 0;
	if (Camera_jpeg_file_size() != 1)
	return 0;
	unsigned char file_size_hex[2];
	file_size_hex[0] = file_size >> 8;
	file_size_hex[1] = file_size;

	if (pass == 1)
	{
		USART_BYTES_Transmit(xbee_usart_num, file_size_hex, sizeof(file_size_hex));
	}
	usart0_count = 0;
	return pass;
}

/**************************************************************************
*                                                                        *
**************************************************************************/

unsigned char motor_control()
{
	unsigned pass = 1;
	/*usart0_count = 0;
	if (usart_mode == 0)
	{
		while (usart0_count != 4)
		{
			USART_Receive(xbee_usart_num);
		}
	}
	else
	{
		while (usart0_count != 4)
		;
	}
	for (unsigned char i = 0; i < 3; i++) */
	for (unsigned char i = 1; i < 3; i++)
	{
		if (myProtocal.motor_direction[i] != usart0_rec_buff[i])
		pass = 0;
	}
	//check the last byte
	if (usart0_rec_buff[3]== 0x01)  //forward
	SPI_MasterTransmit(0x01);
	else if (usart0_rec_buff[3]== 0x02) //reverse
	SPI_MasterTransmit(0x02);
	else if (usart0_rec_buff[3]== 0x03) //left
	SPI_MasterTransmit(0x03);
	else if (usart0_rec_buff[3]== 0x04) //right
	SPI_MasterTransmit(0x04);
	else
	return 0;
	
	usart0_count = 0;
	return pass;
}

/**************************************************************************
*                                                                        *
**************************************************************************/

unsigned char imageReturn()
{
	unsigned pass = 1;
	/*usart0_count = 0;
	if (usart_mode == 0)
	{
		while (usart0_count != 4)
		{
			USART_Receive(xbee_usart_num);
		}
	}
	else
	{
		while (usart0_count != 4)
		;
	}
	for (unsigned char i = 0; i < 4; i++)*/
	for (unsigned char i = 1; i < 4; i++)
	{
		if (myProtocal.take_picture[i] != usart0_rec_buff[i])
		pass = 0;
	}

	if (Camera_Read_jpeg() != 1) //IMAGE TRANFERED FROM HERE
	return 0;
	if (Camera_Stop_taking_pic() != 1)
	return 0;

	usart0_count = 0;
	return pass;
}

/**************************************************************************
*                                                                        *
**************************************************************************/

unsigned char TaskSwitch()
{
	unsigned pass = 1;
	usart0_count = 0;
	if (usart_mode == 0)
	{
		while (usart0_count != 4)
		{
			USART_Receive(xbee_usart_num);
		}
	}
	else
	{
		while (usart0_count != 4)
		;
	}
	
	if (usart0_rec_buff[0]==0x57)
	{
		if (sizeReturn() != 1)
		return 0;
	}
	
	else if (usart0_rec_buff[0]==0x58)
	{
		if (imageReturn() != 1)
		return 0;
	}
	
	else if (usart0_rec_buff[0]==0x59)
	{
		if (motor_control() != 1)
		return 0;
	}
	else
	return 0;
	
	usart0_count = 0;
	return pass;
}


